@extends('layout.master')

@section('title')
   Administrator Page - Update Product
@stop

@section('username')
   {{ Auth::user()->userName }} 
@stop

@section('header')
    @parent
@stop

@section('side-menu')
    @parent
@stop

@section('content')

    <h3> Update Existing Product </h3> <hr />

    <h5> Product Name </h5> <br /> 
    <select size="1" id="product">
        @foreach($products as $product)
            <option value={{$product->id}}> {{ $product->productName }} </option>
        @endforeach
    </select>  <br /> <br />
    
    <input type="button" id="update" value="Update Product" class="btn btn-success" /> <br /> <br /> 
    
    <div class="update" style="visibility: hidden">
    <form id="UpdateForm" action="/api/product/update" method="post">
    		
    		<input type="hidden" name="id" id="id"/>  
		
		    <label> Product Name </label>
		    <input type="text" name="productname" id="productname" /> 
		
		    <label> Cost Price </label>
		    <input type="text" name="costprice"  id="costprice"/>
		
		    <label> Retail Price </label>
		    <input type="text" name="retailprice" id="retailprice"/>      
		
		    <label> Quantity </label>
		    <input type="text" name="quantity" id="quantity"/>  <br />
		
		    <input type="submit" id="update-test" value="Update Product" class="btn btn-success" />   <input type="submit" id="clear" value="Clear Form" class="btn btn-danger" />    
    
    	<form>
    </div>

@stop

@section('scripts')
    <script type="text/javascript">

        jQuery(function(){
                jQuery("#productname").validate({
                    expression: "if (VAL) return true; else return false;",
                    message: "Please enter the Required field"
                });

                jQuery("#costprice").validate({
                    expression: "if (VAL < 0) return false; else return true;",
                    message: "Please enter a value greater then 0"
                });

                jQuery("#retailprice").validate({
                    expression: "if (VAL < 0) return false; else return true;",
                    message: "Please enter a value greater then 0"
                });

                jQuery("#quantity").validate({
                    expression: "if (VAL < 0) return false; else return true;",
                    message: "Please enter a value greater then 0"
                });
                
                jQuery("#costprice").validate({
                    expression: "if (!isNaN(VAL) && VAL) return true; else return false;",
                    message: "Please enter a valid number"
                });
                jQuery("#retailprice").validate({
                    expression: "if (!isNaN(VAL) && VAL) return true; else return false;",
                    message: "Please enter a valid number"
                });
                jQuery("#quantity").validate({
                    expression: "if (!isNaN(VAL) && VAL) return true; else return false;",
                    message: "Please enter a valid number"
                });
                
                jQuery('.AdvancedForm').validated(function(){
                    alert("Use this call to make AJAX submissions.");
                });
            });

        $('#clear').click(function(e) {

            e.preventDefault();
            
            $('#UpdateForm')[0].reset();
            $('#UpdateForm input:first').focus();
        });

        $('#update').on('click', function() {

            var id = $('#product').val();

            $.ajax({

                url: '/api/product/update/' +id,
                type: 'GET',
                success: function(data) {

					$(".update").css("visibility", "visible");
					$("#id").val(data.id);
                    $("#supplier").append("<option value='"+data.id+"'> "+data.Supplier_id+" </option>");
                    $("#category").append("<option value='"+data.id+"'> "+data.Supplier_id+" </option>");
                    $("#productname").val(data.productName);
                    $("#costprice").val(data.costPrice);
                    $("#retailprice").val(data.retailPrice);
                    $("#quantity").val(data.quantity);
                },
                error: function() {

                    alert("Request Failed");
                } 
            });
        });
        
        $("#update-new").click(function() {
        	
        	var product = 
        	{
        		"id": $("#id").val(),
        		"Supplier_id": $("#supplier").val(),
        		"Category_id": $("#category").val(),
        		"productName": $("#productname").val(),
        		"costPrice": $("#costprice").val(),
        		"retailPrice": $("#retailprice").val(),
        		"quantity": $("#quantity").val() 
        	};        	
       
        	
        	$.ajax({
        		
        		url: '/api/product/update',
        		type: 'post',
        		dataType:'JSON',
        		data: { "model" : "3" },
        		success: function(data) {
        			
        			alert(data);
        		},
        		error: function() {
        			
        			alert("Error");
        		}
        	}); 
        });
    </script>
@stop


